#include "logicsystem.h"

LogicSystem::LogicSystem()
{}

LogicSystem& LogicSystem::getInstance()
{
	static LogicSystem instance;
	return instance;
}

void LogicSystem::init()
{
	for (int i = 0; i < std::thread::hardware_concurrency(); ++i) {
		std::shared_ptr<LogicWork> worker = std::make_shared<LogicWork>();
		logicWorks_.push_back(worker);
		worker->start();
	}
}

void LogicSystem::stop()
{
	for (auto worker : logicWorks_) {
		worker->stop();
		worker.reset();
	}
}

bool LogicSystem::postTask(int index, std::shared_ptr<LogicNode> logicNode)
{
	std::lock_guard<std::mutex> locker(workLock_);
	if (index > logicWorks_.size()) {
		qDebug() << "index:" << index << " out of range: " << logicWorks_.size();
		return false;
	}
	return logicWorks_[index]->postTask(logicNode);
}

LogicSystem::~LogicSystem()
{
	for (auto worker : logicWorks_) {
		worker->stop();
		worker.reset();
	}
}
